package com.content.view.Component.Button
{
	import com.library.core.IDisposable;
	import com.library.core.UIObject;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import gs.TweenLite;

	public class HighLightButton extends UIObject implements IDisposable
	{
		// _isMouseDown keep the highlight effect when mouse roll out the button with down state 
		private var _isMouseDown:Boolean = false;
		// _isInside keep the highlight effect when mouse up but still inside the button area
		private var _isInside:Boolean = false;
		
		protected var _normalObj:DisplayObject;
		protected var _highLightObj:DisplayObject;
		
		private var _id:String;
		
		
		public function HighLightButton(normalObj:DisplayObject, highLightObj:DisplayObject)
		{
			super();
			buttonMode = true;
			mouseChildren = false;
			
			_normalObj = normalObj;
			_highLightObj = highLightObj;
			_highLightObj.alpha = 0;
			
			create();
			addEventListeners();
			
		}
		
		protected function create():void
		{
			addChild(_normalObj);
			addChild(_highLightObj);
		}
		
		protected function addEventListeners():void
		{
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			
			addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		}
		
		// Events
		protected function onMouseOver(evt:MouseEvent):void{
			evt.stopImmediatePropagation();
			_isInside = true;
//			Tweener.addTween(_highLightObj, {alpha:1, time:0.6, transition:"easeOutQuint"});			
			TweenLite.to(_highLightObj, 0.6, {alpha:1});
		}
		
		protected function onMouseOut(evt:MouseEvent):void{
			evt.stopImmediatePropagation();
			_isInside = false;
			if(_isMouseDown == false){
//				Tweener.addTween(_highLightObj, {alpha:0, time:0.6, transition:"easeOutQuint"});
				TweenLite.to(_highLightObj, 0.6, {alpha:0});
			}
		}
		protected function onMouseUp(evt:MouseEvent):void{
			_isMouseDown = false;
			if(_isInside == false){
//				Tweener.addTween(_highLightObj, {alpha:0, time:0.6, transition:"easeOutQuint"});
				TweenLite.to(_highLightObj, 0.6, {alpha:0});
			}
			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		protected function onMouseDown(evt:MouseEvent):void{
			_isMouseDown = true;
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		// Get and Set
		public function get id():String					{		return _id;		}
		public function set id(value:String):void		{		_id = value;	}
		
		// Public ///////////////////
		public function Dispose():void{
			removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);			
			removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			removeAllChilds();
		}
		
	}
}